<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row class="addCate">
        <el-button type="primary" @click="showAddCate">添加分类</el-button>
      </el-row>
      <tree-table
        class="treeTable"
        :data="goodsList"
        show-index
        border
        :expand-type="false"
        :selection-type="false"
        :columns="columns"
        :show-row-hover="false"
      >
        <!-- 是否有效的插槽 -->
        <template slot="isYx" slot-scope="scope">
          <i
            class="el-icon-success"
            style="color: lightgreen"
            v-if="scope.row.cat_deleted === false"
          ></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <!-- 排序的插槽 -->
        <template slot="sort" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <!-- 操作插槽 -->
        <template slot="operate" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!-- 分页部分 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 添加分类的弹出框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialog"
      width="50%"
      :before-close="closeAddCateDialog"
    >
      <el-form :model="addCateForm" :rules="addCateRule" ref="addCateRef" label-width="100px">
        <el-form-item label="分类名称：" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <el-cascader
            v-model="value"
            :options="parentCateList"
            :props="setProps"
            @change="selectParent"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAddCateDialog">取 消</el-button>
        <el-button type="primary" @click="suerAddCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getGoodsListApi, addCateApi } from "../api/index.js";
export default {
  data: function() {
    return {
      // 获取商品列表需要的参数
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表
      goodsList: [],
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isYx"
        },
        {
          label: "排序",
          type: "template",
          template: "sort"
        },
        {
          label: "操作",
          width: "200px",
          type: "template",
          template: "operate"
        }
      ],
      // 商品列表总数
      total: 0,
      // 控制添加分类的弹出框的显示与隐藏
      addCateDialog: false,
      // 添加分类的数据
      addCateForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      // 添加分类的验证规则
      addCateRule: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' },]
      },
      // 父级分类的数据
      parentCateList: [],
      // 父级分类联机选择器的props配置
      setProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true
      },
      value: []
    };
  },
  created() {
    this.getAllGoodsList();
    this.getAllParentList();
  },
  methods: {
    async getAllGoodsList() {
      const { data: res } = await getGoodsListApi(this.queryInfo);
      if (res.meta.status !== 200)
        return this.$message.error("获取商品列表失败");
      this.goodsList = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getAllGoodsList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getAllGoodsList();
    },
    // 添加分类的业务代码
    showAddCate() {
      this.addCateDialog = true;
    },
    // 获取父级分类列表
    async getAllParentList() {
      let obj = {type: 2}
      const { data: res } = await getGoodsListApi(obj);
      console.log(res);
      if (res.meta.status !== 200) return;
      this.parentCateList = res.data;
    },
    selectParent(value) {
      console.log(value);
      if (value.length > 0) {
        this.addCateForm.cat_pid = value[value.length - 1];
        this.addCateForm.cat_level = value.length;
      } else {
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
      }
    },
    suerAddCate() {
      this.$refs.addCateRef.validate(async valid => {
        if(!valid) return
        const {data: res} = await addCateApi(this.addCateForm)
        if(res.meta.status !== 201) return this.$message.error('添加分类失败！')
        this.$message.success('添加分类成功！')
        this.getAllGoodsList();
        this.closeAddCateDialog()
        this.addCateDialog = false
      })
    },
    closeAddCateDialog() {
      this.addCateDialog = false
      this.$refs.addCateRef.resetFields();
      this.value = []
    }
  }
};
</script>

<style lang="less" scoped>
.addCate {
  margin-bottom: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
